require("./index.scss");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Template = {
    buildings: ParseTemplate(require("./tpl/step1.xtpl")),
    //step2: ParseTemplate(require("./tpl/step2.xtpl")),
    step3: ParseTemplate(require("./tpl/step3.xtpl"))
    // step4: ParseTemplate(require("./tpl/step4.xtpl"))
}
var XlsUploader = require("../common/xlsupload");
var Main = ZFT.Util.Class({
    container: $("body"),
    init: function () {
        var _this = this;
        //_this.showStepFour();
        _this.downloadTemplate();
    },
    EVENTS: {
        "change #downFileType": "changeFileType",
        "click .u-building-item": "getBuildingTemplate",
        "change #upFileType": "showStepFour"

    },
    loadBuildings: function () {
        var buildingBox = $("#buildingBox");
        Ajax("/r/Building_getOwnerBuilding", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.buildings({ data: res.data });
                    $("#stepBox2,#stepBox3").removeClass("z-dis-n");
                    buildingBox.html(html);
                } else {
                    var empty = '<span class="i-b p100 z-t-a-c">' + res.msg + ' :-)</span>'
                    buildingBox.html(empty);
                    $("#stepBox2").removeClass("z-dis-n");
                    $("#stepBox3").addClass("z-dis-n");
                    $("#stepBox4 .u-circle").text("3");
                }
            }
        })
    },
    changeFileType: function (e) {
        var tarOpt = $(e.currentTarget),
            _this = this,
            tarType = tarOpt.val();
        $("#upFileType").val(-1);
        if (tarType == 1) {
            $("#stepBox2").addClass("z-dis-n");
            $("#stepBox3").removeClass("z-dis-n");
            $("#buildingBox").html("");
            $("#stepBox3 .u-circle").text("2");
            $("#stepBox4 .u-circle").text("3");
        } else {
            $("#setpBox2").removeClass("z-dis-n");
            _this.loadBuildings();
            $("#stepBox3 .u-circle").text("3");
            $("#stepBox4 .u-circle").text("4");
        }
    },
    getBuildingTemplate: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var id = tarBtn.attr("data-id");
        tarBtn.addClass("chked").siblings("").removeClass("chked");
        _this.downloadTemplate(id);
    },
    downloadTemplate: function (id) {
        var regId = /^\d+$/;
        var _this = this;
        var downLoadUrl = { 1: "/r/MemberImport_exportModel", 2: "/r/PropertyManager_exportModel", 3: "/r/BuildingImport_exportModel", 4: "/r/LeaseImport_exportModel" };//1,用户2,水电3,房源4,租赁合同
        $("#downloadBtn").on("click", function (e) {
            var downFileType = $("#downFileType").val();
            var url = "";
            var u = downLoadUrl[downFileType];
            var building_id = (id) ? "?building_id=" + id : ""
            if (!downFileType || !regId.test(downFileType)) return STip("fail", "请勿修改模板文件类型！");
            url = u + building_id;
            window.open(url, "_self");
        })
    },
    showStepFour: function (e) {
        var _this = this,
            tarType = $(e.currentTarget).val();
        step4 = Template.step3({ type: tarType });
        $("#stepFourBox").html((tarType == -1) ? "" : step4);
        if (tarType != -1) {
            _this.initXlsUploader(1);
            $("#checkDataBtn").on("click", function (e) {
                _this.checkData(e,tarType);
            });
        }

    },
    initXlsUploader: function (i) {
        var _this = this;
        var uploadername = $("#xlsLoaderContainer_" + i).find("input[name=callback]").val();
        if (uploadername) {
            uploadername = uploadername.split(".")[0];
            window[uploadername] = new XlsUploader({ container: $("#xlsLoaderContainer_" + i) });
        }
    },
    checkData: function (e,type) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var xlsInp = $("#xlsInp");
        //var upFileType = $("#upFileType").val();
        var url = xlsInp.val();
        var filename = xlsInp.attr("data-name");
        var Url={1:"/r/MemberImport_readExcel",2: "/r/PropertyManager_readExcel", 3: "/r/BuildingImport_readExcel", 4: "/r/LeaseImport_readExcel" };//1,用户2,水电3,房源4,租赁合同
        $("#tipBox").addClass("z-dis-n");
        if (!url || !filename) return STip("fail", "请先上传附件！", 4000);
        var checkRequest = Ajax(Url[type], {
            type: "POST",
            dataType: "json",
            params: {
                file: url,
                filename: filename
            },
            loading: function () {
                $("#checkLoadBox").css("display", "inline-block");
                tarBtn.attr("disabled", "disabled");
                setTimeout(function () {
                    STip("success", "文件校验成功！");
                    $("#fileTitle").text("");
                    $("#checkLoadBox").css("display", "none");
                    $("#tipBox").removeClass("z-dis-n");
                }, 3000)
            },
            complete: function () {
                $("#checkLoadBox").css("display", "none");
            },
            success: function (res) {
                if (res.code == 200 || res.code == 201) {
                    res["file_url"] = res.data.url;
                    //_this.showStepFour(res);
                    tarBtn.css("display", "none");
                    STip("success", "文件校验成功！");
                } else {
                    tarBtn.removeAttr("disabled");
                    xlsInp.val("").attr("data-name", "");
                    return STip("fail", res.msg);
                }
            }
        })
    },

});

$(function () {
    new Main();
})